<?php
/**
 * @copyright meila.com
 * @author zhanglei11@
 * @name 
 * @param 
 *         $xxx = 作用
 * @static 
 *         加关注队列
 * 
 * 
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');

class ml_mq_add_follow_meila extends MqClass{
    const QUEUE_NAME = ML_QUEUENAME_ADD_FOLLOW_MEILA;
    private $_oFollow;
    private $_oFans;
    private $_oRedis;
    const MEILA='5211519'; //美啦官方uid
    const MEILA_WEIBO ='2459731433';//美啦官方微博uid

    public function __construct($queue)
    {
        $this->_oFollow = new ml_model_dbFollow();
        $this->_oFans = new ml_model_dbFans();
        
        parent::__construct($queue);
    }

    public function run_job() {        
        //接收的数据
        $arr = $this->src_data;
        if(empty($arr['uid'])){
            return false;
        }

        $arr['follow_uid']=self::MEILA;
        $arr['wb_id']=self::MEILA_WEIBO;

        $this->follow_meila($arr);
        if(!empty($arr['3rd_id']) && $arr['server_id']==1  ){
            $this->follow_meila_weibo($arr);
        }

        return true;
    }
    /**
     * 关注美啦
     * @param $arr
     * @return bool
     */
    public function follow_meila($arr){
        $this->_oFollow->isFollow($arr['uid'],$arr['follow_uid']);
        if($this->_oFollow->get_data()){
            return true;//已经关注过
        }
        if($this->_oFollow->addFollow($arr['uid'], $arr['follow_uid'])){
            $this->_oFans->addFans($arr['follow_uid'], $arr['uid']);
            //增加redis    关注数
            $this->_oRedis = new ml_model_rdsCount();
            $this->_oRedis->incrFollowCnt($arr['uid']);            //    增加 当前用户的关注数
            $this->_oRedis->incrFansCnt($arr['follow_uid']);    //    增加关注人的粉丝数

            ml_tool_sendMq::add_atnick_follow($arr['uid'], $arr['follow_uid']);
            return true;
        }else{
            return false;
        }
    }
    /**
     * 关注美啦weibo
     * @param $data
     * @return bool
     */
    public function follow_meila_weibo($data){
        if(empty($data['access_token'])){
            $o3rd = new ml_model_db3rdService();
            $rs = $o3rd -> get3rdServiceUser($data['uid'],1 ,$serData);
            if(!$rs){
                return false;
            }
            $data['access_token']=$serData['access_token'];
        }

        $weibo=new ml_model_openapiWeibo();

        $re=$weibo->is_followed($data);
        if($re){
            return true;
        }
        $rs=$weibo->follow_uid($data);
        if($rs){
            return true;
        }else{
            return false;
        }
    }
}

ml_run_queue_check(ml_mq_add_follow_meila::QUEUE_NAME);
$xblog_obj = new ml_mq_add_follow_meila(new RsQueue(ml_mq_add_follow_meila::QUEUE_NAME));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();
?>